#include "utils.h"

char __full_time_format[] = "%F %R";
char __date_time_format[] = "%F";
int  __time_criteria = 8; /* Count automatically from mask later */

time_t str_to_time(const char * source,
                   int left_chop, int right_chop)
{
    struct tm tm;
    char __buffer[65535];
    int i,j;

    int source_length = 0;

    source_length = strlen(source);

    if (right_chop + left_chop >= source_length)
        return -1;

    int is_space = 0;
    int digits = 0;
    char * ptr = 0;

    
        
    /* Smart copy */
    for (i=left_chop, j=0; i<source_length - right_chop; i++)
    {
        
        
        switch(source[i])
        {
            case ' ': if (is_space) break;
                      is_space = 1;
                      __buffer[j++] = source[i];
                      break;
            case '0': 
            case '1': 
            case '2': 
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9': digits ++;
            case '-':
            case '/':
            case ':':
                is_space = 0;
                __buffer[j++] = source[i];
                break;
        }
    }
    __buffer[j] = 0x0;

    /* Delete trailing spaces */
    for (--j; j>0; j--)
        if ((__buffer[j]==' ')
            || (__buffer[j]=='\t'))
            __buffer[j] = 0x0;
        else break;

    memset(&tm,0x0,sizeof(struct tm));
    
    /* Use proper mask */
    if (digits > __time_criteria)
    {
        ptr = strptime(__buffer,__full_time_format,&tm);
        printf("|DATE: %s:%s:%s|", __buffer,__full_time_format,ptr);
    }
    
    else
    {
        ptr = strptime(__buffer,__date_time_format,&tm);
        printf("|DATE: %s:%s:%s|", __buffer,__date_time_format,ptr);
    }

    tm.tm_isdst = -1;

    time_t time = mktime(&tm);

    printf("|TIME:%lld; y:%d, m:%d, d:%d|", time, tm.tm_year, tm.tm_mon, tm.tm_mday);
    

    return time;
}

#define BLOCK 10

char * append_string(char * dest, const char * src)
{
    int new_length = (dest ? strlen(dest) : 0)
                   + (src  ? strlen(src)  : 0)
                   + 2;

#if 0 
    dest = realloc(dest, new_length);
#else
    /* Just test thi smart shit */
    /* You can damage data, if put anything there by your fucked hands. */
    
    if (! dest)
    {
        new_length = (new_length / BLOCK) * BLOCK + BLOCK;
        dest = malloc(new_length);
        memset(dest,0x0,new_length);
    }
    else
    {
        if ((new_length / BLOCK) != (strlen(dest) / BLOCK))
        {
            new_length = (new_length / BLOCK) * BLOCK + BLOCK;
            dest = realloc(dest, new_length);
        }
        
    }
#endif 

    if (! dest)
        return NULL;
        
    return strcat(dest, src);
}


int is_keyword(const char * word, const char * list[])
{
    int i;
    
    for (i = 0; list[i]; i++)
    {
        if (strcmp(word, list[i]) == 0)
        {
            printf("(K!)");
            return 1;
        }
    }
    return 0;
}

int str_to_completion(const char * completion)
{
    /* Completion box consists of two parts */

    int result = 0x0;
    int i;
    int acc = 0;

    for (i=0; i<strlen(completion); i++)
    {
        switch(completion[i])
        {
            case '0': acc = acc*10 + 0; break;
            case '1': acc = acc*10 + 1; break; 
            case '2': acc = acc*10 + 2; break;  
            case '3': acc = acc*10 + 3; break;  
            case '4': acc = acc*10 + 4; break;  
            case '5': acc = acc*10 + 5; break;  
            case '6': acc = acc*10 + 6; break;  
            case '7': acc = acc*10 + 7; break;  
            case '8': acc = acc*10 + 8; break;  
            case '9': acc = acc*10 + 9; break;  
            case '/':
            case ']':
                result = result
                ? ((result << sizeof(result)*4) | acc)
                : acc;
                acc = 0;
                break;
        }
    }
    return result;
 
}


